<?php

require_once APPPATH."/controllers/BasePriv.php";
require APPPATH."/libraries/FFmpeg.php";
require_once APPPATH."/libraries/Tools.php";
use Aws\S3\S3Client;
class Activity_material extends BasePriv
{
    public function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', "url"));
        $this->load->model("Host_add_activity_model", "table");
        $this->add_activity = "add_activity";
    }

    /**
     * 推荐活动视频主页
     */
    public function index(){
        //分页
        $arr=[];
        $arr['page']=intval($this->get('page'))>0 ? intval($this->get('page')) : 1;
        $arr['limit']=intval($this->get('limit'))>0 ? intval($this->get('limit')) : 10;

        //查询数据（原始素材）
        $res=$this->table->all($arr);
        //查询分类
        $get_fenlei=$this->table->get_fenlei(2);
        //查询话题
        $get_huati=$this->table->get_huati("",3);
        //显示话题和分类
        if(!empty($res['row'])){
            foreach($res['row'] as $k=>&$v){
                foreach($get_fenlei as $kk=>$vv){
                    if($v['fenlei']==$vv['id']){
                        $v['fenlei_name']=$vv['name'];
                    }
                }
                foreach($get_huati as $kkk=>$vvv){
                    if($v['huati']==$vvv['id']){
                        $v['huati_name']=$vvv['sub_title'];
                    }
                }
                if($v['sort']==999999999){
                    $v['sort']=0;
                }
            }
        }
        $data['data']=$res;
        $data['page']=$arr['page'];
        $data['limit']=$arr['limit'];
        $this->layout("material/activity_material/index",$data);
    }

    /**
     * 新增活动视频页
     */
    public function add_activity(){
        $this->layout("material/activity_material/add_activity");
    }

    /**
     * 保存活动视频
     */
    public function save_add_activity(){
        if(empty($_POST['fenlei'])){
            response(-1,"请选择分类");
        }
        if(empty($_POST['huati'])){
            response(-1,"请选择话题");
        }
        if(empty($_POST['video_url'])){
            response(-1,"请添加视频");
        }
        if(empty($_POST['hengping_url'])){
            response(-1,"请添加横屏封面图");
        }
        if(empty($_POST['share_url'])){
            response(-1,"请添加分享封面图");
        }
        if(empty($_POST['video_title'])){
            response(-1,"请添加视频标题");
        }
        $ffmpeg = new FFmpeg();
        if(empty($_POST['shuping_url'])){
            //用ffmpeg截取视频第一帧作为封面
            $image_dir=$this->upload_dir();
            $true=$ffmpeg->getCoverFromVideo($_POST['video_url'],$image_dir['uploadpath']);
            if($true){
                $data['shuping_pic']=$image_dir['savehttp'];
            }
        }else{
            $data['shuping_pic']=$_POST['shuping_url'];
        }
        //获取视频时长
        $data['video_time']=$ffmpeg->getVideoLen($_POST['video_url']);
        $data['video_uuid']=Tools::UUID();
        $data['fenlei']=$_POST['fenlei'];
        $data['huati']=$_POST['huati'];
        $data['video_title']=$_POST['video_title'];
        $data['hengping_pic']=$_POST['hengping_url'];
        $data['fenxiang_pic']=$_POST['share_url'];
        $data['video_url']=$_POST['video_url'];
        $data['add_time']=date("Y-m-d H:i:s",time());
        $data['update_time']=date("Y-m-d H:i:s",time());
        $res=$this->table->add($this->add_activity,$data);
        if($res){
            response(0,"添加成功",base_url("material/activity_material/edit_add_activity?id={$res}"));
        }else{
            response(-1,"添加失败");
        }
    }

    /**
     * 修改
     */
    public function edit_add_activity(){
        $id=$_REQUEST['id'];
        if(empty($id)){
            response(-1,"缺少参数");
        }

        $res=$this->table->one($this->add_activity,['id'=>$id]);
        //获取分类
        $fenlei=$this->table->get_fenlei();
        //获取话题
        $huati=$this->table->get_huati($res['fenlei']);
        $data['data']=$res;
        $data['fenlei']=$fenlei;
        $data['huati']=$huati;
        $this->layout("material/activity_material/edit_add_activity",$data);
    }

    /**
     * 保存修改
     */
    public function save_edit_add_activity(){
        if(empty($_POST['id'])){
            response(-1,"缺少参数");
        }
        if(empty($_POST['fenlei'])){
            response(-1,"请选择分类");
        }
        if(empty($_POST['huati'])){
            response(-1,"请选择话题");
        }
        if(empty($_POST['video_url'])){
            response(-1,"请添加视频");
        }
        if(empty($_POST['hengping_url'])){
            response(-1,"请添加横屏封面图");
        }
        if(empty($_POST['share_url'])){
            response(-1,"请添加分享封面图");
        }
        if(empty($_POST['video_title'])){
            response(-1,"请添加视频标题");
        }
        $ffmpeg = new FFmpeg();
        if(empty($_POST['shuping_url'])){
            //用ffmpeg截取视频第一帧作为封面
            $image_dir=$this->upload_dir();
            $true=$ffmpeg->getCoverFromVideo($_POST['video_url'],$image_dir['uploadpath']);
            if($true){
                $data['shuping_pic']=$image_dir['savehttp'];
            }
        }else{
            $data['shuping_pic']=$_POST['shuping_url'];
        }
        //获取视频时长
        $data['video_time']=$ffmpeg->getVideoLen($_POST['video_url']);
        $data['fenlei']=$_POST['fenlei'];
        $data['huati']=$_POST['huati'];
        $data['video_title']=$_POST['video_title'];
        $data['hengping_pic']=$_POST['hengping_url'];
        $data['fenxiang_pic']=$_POST['share_url'];
        $data['video_url']=$_POST['video_url'];
        $data['update_time']=date("Y-m-d H:i:s",time());
        $res=$this->table->update($this->add_activity,$data,['id'=>$_POST['id']]);
        if($res){
            response(0,"修改成功",base_url("material/activity_material/edit_add_activity?id={$res}"));
        }else{
            response(-1,"修改失败");
        }
    }

    /**
     * 停用
     */
    public function stop(){
        $id=$_POST['id'];
        if(empty($id)){
            response(-1,"缺少参数");
        }
        $data['online']=1;
        $res=$this->table->update($this->add_activity,$data,['id'=>$id]);
        if($res){
            response(0,"停用成功");
        }else{
            response(-1,"停用失败");
        }
    }

    /**
     * 启用
     */
    public function up(){
        $id=$_POST['id'];
        if(empty($id)){
            response(-1,"缺少参数");
        }
        $data['online']=2;
        $res=$this->table->update($this->add_activity,$data,['id'=>$id]);
        if($res){
            response(0,"启用成功");
        }else{
            response(-1,"启用失败");
        }
    }

    /**
     * 移除
     */
    public function del(){
        $id=$_POST['id'];
        if(empty($id)){
            response(-1,"缺少参数");
        }
        $res=$this->table->delete(['id'=>$id]);
        if($res){
            response(0,"移除成功");
        }else{
            response(-1,"移除失败");
        }
    }

    /**
     * 排序
     */
    public function sort(){
        $id=$_POST['id'];
        $sorts=$_POST['sorts'];
        if(empty($id)){
            response(-1,"缺少参数");
        }
        if(!is_numeric($sorts)){
            response(-1,"排序值不是数字");
        }
        if($sorts<0 || $sorts>999){
            response(-1,"排序值不合理");
        }
        if($sorts==0 || $sorts=="0"){
            $sorts=999999999;
        }
        $res=$this->table->sort($id,$sorts);
        if($res){
            response(0,"排序成功");
        }else{
            response(-1,"排序失败");
        }
    }



    /**
     * 异步获取分类
     */
    public function get_fenlei(){
        $res=$this->table->get_fenlei();
        response(0,"查询成功",$res);
    }

    /**
     * 异步获取话题
     */
    public function get_huati(){
        $huati_id=$_POST['huati_id'];
        if(empty($huati_id)){
            response(-1,"获取失败，缺少参数");
        }
        $res=$this->table->get_huati($huati_id);
        response(0,"查询成功",$res);
    }

}